package logic

import (
	"context"
	"login-guard/models/mysql"
	"login-guard/pkg"

	"login-guard/loginrpc/internal/svc"
	"login-guard/loginrpc/loginrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *loginrpc.AddRequest) (*loginrpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	var findAdd mysql.Login
	mysql.DB.Debug().Where("account = ?", in.Account).Find(&findAdd)
	data := mysql.Login{

		Account:  in.Account,
		Password: pkg.MD5(in.Password),
	}
	err := mysql.DB.Debug().Create(&data).Error
	if err != nil {
		return nil, err
	}
	return &loginrpc.AddResponse{
		LoginId: int64(findAdd.ID),
	}, nil
}
